package com.androidbegin.jsonparsetutorial;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.androidbegin.jsonparsetutorial.*;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SingleItemView extends Activity {
	// Declare Variables
	String id;
	String content;
	String img;
	static String IMAGE = "src";
	String position;
	ProgressDialog pDialog;
	ArrayList<HashMap<String, String>> arraylist;
	ListView ltvView;
	ImageView imgView;
	ViewAdapter adapter;
	JSONObject jsonobject;
	JSONArray jsonarray;
	boolean click1;
	Matrix matrix = new Matrix();
	Matrix savedMatrix = new Matrix();
	PointF startPoint = new PointF();
	PointF midPoint = new PointF();
	float oldDist = 1f;
	static final int NONE = 0;
	static final int DRAG = 1;
	static final int ZOOM = 2;
	int mode = NONE;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.singleitemview);
		Intent i = getIntent();
		id = "http://service3.topprinter.org/phototamtayvn-rest/"
				+ i.getStringExtra("id");
		Log.i("id", id);
		ltvView = (ListView) findViewById(R.id.listview);
		ltvView.setFocusable(false);
		imgView = (ImageView) findViewById(R.id.ivImage);
	
		new DownloadJSON().execute();
	}

	// DownloadJSON AsyncTask
	private class DownloadJSON extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// Create a progressdialog
			pDialog = new ProgressDialog(SingleItemView.this);
			// Set progressdialog message
			pDialog.setMessage("Loading...");
			pDialog.setIndeterminate(false);
			// Show progressdialog

			pDialog.show();
		}

		@Override
		protected Void doInBackground(Void... params) {
			Log.i("doInBackground - id:", id);
			arraylist = new ArrayList<HashMap<String, String>>();
			ServiceHandler sh = new ServiceHandler();
			String jsonStr = sh.makeServiceCall(id, ServiceHandler.GET);
			Log.i("doInBackground - jsonStr:", jsonStr);
			try {
				JSONObject jsonObj = new JSONObject(jsonStr);
				jsonarray = jsonObj.getJSONArray("data");

				for (int i = 0; i < jsonarray.length(); i++) {
					HashMap<String, String> map = new HashMap<String, String>();
					jsonobject = jsonarray.getJSONObject(i);
					map.put("src", jsonobject.getString("src"));
					arraylist.add(map);
				}
			} catch (JSONException e) {
				Log.e("Error", e.getMessage());
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void args) {
			ltvView = (ListView) findViewById(R.id.listview);
			Log.i("onPostExecute - ltview:", ltvView.toString());
			adapter = new ViewAdapter(SingleItemView.this, arraylist);
			Log.i("onPostExecute - adapter:", adapter.toString());
			ltvView.setAdapter(adapter);
			pDialog.dismiss();

		}
	}
}